package com.jeecg.demo.service.impl;

import com.jeecg.demo.entity.XmStockinfoEntity;
import com.jeecg.demo.entity.XmUsableinfoEntity;
import com.jeecg.demo.service.XmStockinfoServiceI;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.MyBeanUtils;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.core.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service("xmStockinfoService")
@Transactional
public class XmStockinfoServiceImpl extends CommonServiceImpl implements XmStockinfoServiceI {

	@Autowired
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
 	public void delete(XmStockinfoEntity entity) throws Exception{
 		super.delete(entity);
 	}
	public void addMain(XmStockinfoEntity xmStockinfo,
	        List<XmUsableinfoEntity> xmUsableinfoList) throws Exception{
			//保存主信息
			this.save(xmStockinfo);
			if(xmUsableinfoList != null){
				XmUsableinfoEntity xmUsableinfo = xmUsableinfoList.get(0);
				xmUsableinfo.setStockId(xmStockinfo.getId());
				xmUsableinfo.setCreateTime(new Date());
				xmUsableinfo.setLastyear(xmUsableinfo.getLastyear());
				xmUsableinfo.setUnit("天");
				xmUsableinfo.setCurrentuse(xmUsableinfo.getCurrentuse());
				xmUsableinfo.setYearday(xmUsableinfo.getYearday());
				xmUsableinfo.setLastyearday(xmUsableinfo.getLastyearday());
				this.save(xmUsableinfo);
			}
			/**保存-可用天数图表信息*/
			//for(XmUsableinfoEntity xmUsableinfo:xmUsableinfoList){
				//外键设置

			//}
	}

	public void updateMain(XmStockinfoEntity xmStockinfo,
	        List<XmUsableinfoEntity> xmUsableinfoList) throws Exception {
		//保存主表信息
		if(StringUtil.isNotEmpty(xmStockinfo.getId())){
			try {
				XmStockinfoEntity temp = findUniqueByProperty(XmStockinfoEntity.class, "id", xmStockinfo.getId());
				MyBeanUtils.copyBeanNotNull2Bean(xmStockinfo, temp);
				this.saveOrUpdate(temp);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}else{
			this.saveOrUpdate(xmStockinfo);
		}
		//===================================================================================
		//获取参数
		Object id0 = xmStockinfo.getId();
		//===================================================================================
		//1.查询出数据库的明细数据-可用天数图表信息
	    String hql0 = "from XmUsableinfoEntity where 1 = 1 AND stockId = ? ";
	    List<XmUsableinfoEntity> xmUsableinfoOldList = this.findHql(hql0,id0);
		//2.筛选更新明细数据-可用天数图表信息
		if(xmUsableinfoList!=null&&xmUsableinfoList.size()>0){
		for(XmUsableinfoEntity oldE:xmUsableinfoOldList){
			boolean isUpdate = false;
				for(XmUsableinfoEntity sendE:xmUsableinfoList){
					//需要更新的明细数据-可用天数图表信息
					if(oldE.getId().equals(sendE.getId())){
		    			try {
							MyBeanUtils.copyBeanNotNull2Bean(sendE,oldE);
							this.saveOrUpdate(oldE);
						} catch (Exception e) {
							e.printStackTrace();
							throw new BusinessException(e.getMessage());
						}
						isUpdate= true;
		    			break;
		    		}
		    	}
	    		if(!isUpdate){
		    		//如果数据库存在的明细，前台没有传递过来则是删除-可用天数图表信息
		    		super.delete(oldE);
	    		}

			}
			//3.持久化新增的数据-可用天数图表信息
			for(XmUsableinfoEntity xmUsableinfo:xmUsableinfoList){
				if(oConvertUtils.isEmpty(xmUsableinfo.getId())){
					//外键设置
					xmUsableinfo.setStockId(xmStockinfo.getId());
					this.save(xmUsableinfo);
				}
			}
		}
	}

	public void delMain(XmStockinfoEntity xmStockinfo) throws Exception{
		//删除主表信息
		this.delete(xmStockinfo);
		//===================================================================================
		//获取参数
		Object id0 = xmStockinfo.getId();
		//===================================================================================
		//删除-可用天数图表信息
	    String hql0 = "from XmUsableinfoEntity where 1 = 1 AND stockId = ? ";
	    List<XmUsableinfoEntity> xmUsableinfoOldList = this.findHql(hql0,id0);
		this.deleteAllEntitie(xmUsableinfoOldList);

	}

}
